require "scripts.少年光头.Helper.deepcopy"
require "scripts.少年光头.UiHelper.UiHelper"
require "scripts.少年光头.Helper.hlua.hweather"
---天气系统
---@param time number 多少秒刷新一次
surroundings = require "scripts.少年光头.Helper.classic":extend()
surroundings.info = {
    RefreshTime = 60,
    RefreshTimeID = nil,
    ---当前季节
    season = "",
    ---当前环境
    weather = "",
    ---当前环境天气ID
    weatherID = {}
}
surroundings.season = {
    ["春"] = {},
    ["夏"] = {},
    ["秋"] = {},
    ["冬"] = {}
}
surroundings.weather = {
    ["日光"] = {
        key = {"sun"},
        ---事件
        Event = {}
    },
    ["月光"] = {
        key = {"moon"},
        ---事件
        Event = {}
    },
    ["紫光盾"] = {
        key = {"shield"},
        ---事件
        Event = {}
    },
    ["雨"] = {
        key = {"rain"},
        ---事件
        Event = {}
    },
    ["大雨"] = {
        key = {"rainstorm"},
        ---事件
        Event = {}
    },
    ["雪"] = {
        key = {"snow"},
        ---事件
        Event = {}
    },
    ["大雪"] = {
        key = {"snowstorm"},
        ---事件
        Event = {}
    },
    ["风"] = {
        key = {"wind"},
        ---事件
        Event = {}
    },
    ["大风"] = {
        key = {"windstorm"},
        ---事件
        Event = {}
    },
    ["白雾"] = {
        key = {"mistwhite"},
        ---事件
        Event = {}
    },
    ["绿雾"] = {
        key = {"mistgreen"},
        ---事件
        Event = {}
    },
    ["蓝雾"] = {
        key = {"mistblue"},
        ---事件
        Event = {}
    },
    ["红雾"] = {
        key = {"mistred"},
        ---事件
        Event = {}
    }
}
---初始化
function surroundings:new(time)
    time = time or 60
    self:UI()
    self:SetSeason()
    self:Setweather()
    self:Refresh(time)
    self:RefreshGameTime()
end
---刷新游戏时间
function surroundings:RefreshGameTime()
    htime.setInterval(
        0.01,
        function()
            self:timeLB_txt(string.format("%.2f", htime.timeOfDay(nil)))
        end
    )

    ---string.format(“%.2f”, x)
end
---刷新
function surroundings:Refresh(time)
    if self.info.RefreshTimeID ~= nil then
        self.info.RefreshTimeID.destroy()
    end
    self.info.RefreshTimeID =
        htime.setInterval(
        time,
        function(curTimer)
            echo(htime.timeOfDay(nil))
            ---self:SetSeason(nil)
            self:Setweather(nil)
        end
    )
end

---季节变更
---@param season string 季节 默认nil随机 春 夏 秋 东
---@return surroundings
function surroundings:SetSeason(season)
    local seasonName = season
    if season == nil then
        local seasons = {}
        for key, value in pairs(surroundings.season) do
            table.insert(seasons, key)
        end
        seasonName = seasons[math.random(1, #seasons)]
    end
    self:seasonLB_txt(seasonName)
    self:SetseasonAction(seasonName)
    return self
end
---执行季节事件
---@param season string 执行指定季节事件 春 夏 秋 东
function surroundings:SetseasonAction(season)
    if season == nil then
        print_mbr("SetAction 无指定季节 不执行")
        return
    end
    for index, value in ipairs(self.season[season]) do
        value()
    end
end

---添加季节事件
---@param season string 季节 春 夏 秋 东
---@param action function 季节事件 function () end
function surroundings:SeasonAddAction(season, action)
    if (action ~= nil and type(action) == "function") then
        table.insert(self.season[season], action)
    end
    return self
end

---环境变更
---@param weather string 环境 默认nil随机
---@return surroundings
function surroundings:Setweather(weather)
    local weatherName = weather
    if weather == nil then
        local weathers = {}
        for key, value in pairs(self.weather) do
            table.insert(weathers, key)
        end
        weatherName = weathers[math.random(1, #weathers)]
    end
    ---删除天气
    for i, v in ipairs(self.info.weatherID) do
        hweather.del(v, 0)
    end
    self.info.weatherID = {}
    self:weatherLB_txt(weatherName)
    self:SetweatherAction(weatherName)

    for i, v in ipairs(self.weather[weatherName].key) do
        local id =
            hweather.create({x = 0, y = 0, w = 0, h = 0, whichRect = hrect.world(), type = hweather[v], during = 0})
        table.insert(self.info.weatherID, id)
    end
    return self
end

---执行环境事件
---@param season string 执行指定环境事件
function surroundings:SetweatherAction(weather)
    if weather == nil then
        print_mbr("SetweatherAction 无指定环境 不执行")
        return
    end
    for index, value in ipairs(self.weather[weather].Event) do
        value()
    end
end

---添加环境事件
---@param weather string 环境
---@param action function 环境事件 function () end
function surroundings:weatherAddAction(weather, action)
    if (action ~= nil and type(action) == "function") then
        table.insert(self.weather[weather].Event, action)
    end
    return self
end

function surroundings:UI()
    ---季节
    self.seasonBG =
        UiHelper.backdrop({fdfName = "sysItemTip"}):dfpointp(0, -0.02, nil, FRAME_ALIGN_LEFT_TOP, FRAME_ALIGN_LEFT_TOP):dfsize(
        0.015,
        0.02
    )
    self.seasonLB =
        UiHelper.label({parent = self.seasonBG.frameId}):dfpointp(
        0,
        0,
        self.seasonBG.frameId,
        FRAME_ALIGN_CENTER,
        FRAME_ALIGN_CENTER
    ):dfsize(0.015, 0.02):dftext("春"):dfsetTextAlignment(TEXT_ALIGN_CENTER)

    ---环境
    self.weatherBG = UiHelper.backdrop({fdfName = "sysItemTip"})
    self.weatherLB =
        UiHelper.label():dfpointp(0.004, -0.045, nil, FRAME_ALIGN_LEFT_TOP, FRAME_ALIGN_LEFT_TOP):dftext("日光")
    self.weatherBG:dfpoint(-0.005, 0.005, FRAME_ALIGN_LEFT_TOP, self.weatherLB.frameId, FRAME_ALIGN_LEFT_TOP):dfpoint(
        0.005,
        -0.005,
        FRAME_ALIGN_RIGHT_BOTTOM,
        self.weatherLB.frameId,
        FRAME_ALIGN_RIGHT_BOTTOM
    )

    ---魔兽时间
    self.timeBG = UiHelper.backdrop({fdfName = "sysItemTip"})
    self.timeLB =
        UiHelper.label():dfpointp(0.004, -0.065, nil, FRAME_ALIGN_LEFT_TOP, FRAME_ALIGN_LEFT_TOP):dftext("1:00")
    self.timeBG:dfpoint(-0.005, 0.005, FRAME_ALIGN_LEFT_TOP, self.timeLB.frameId, FRAME_ALIGN_LEFT_TOP):dfpoint(
        0.005,
        -0.005,
        FRAME_ALIGN_RIGHT_BOTTOM,
        self.timeLB.frameId,
        FRAME_ALIGN_RIGHT_BOTTOM
    )

    ---属性
    self.attrBG = UiHelper.backdrop({fdfName = "sysItemTip"})
    self.attrLB = UiHelper.label():dfpointp(0.004, -0.085, nil, FRAME_ALIGN_LEFT_TOP, FRAME_ALIGN_LEFT_TOP):dftext("加成"):dfhide()
    self.attrBG:dfpoint(-0.005, 0.005, FRAME_ALIGN_LEFT_TOP, self.attrLB.frameId, FRAME_ALIGN_LEFT_TOP):dfpoint(
        0.005,
        -0.005,
        FRAME_ALIGN_RIGHT_BOTTOM,
        self.attrLB.frameId,
        FRAME_ALIGN_RIGHT_BOTTOM
    )
end

---修改季节txt
function surroundings:seasonLB_txt(txt)
    self.info.season = txt
    self.seasonLB:dftext(txt)
end
---修改环境txt
function surroundings:weatherLB_txt(txt)
    self.info.weather = txt
    self.weatherLB:dftext(txt)
end
---修改魔兽时间txt
function surroundings:timeLB_txt(txt)
    self.timeLB:dftext(txt)
end
---修改属性txt
function surroundings:attrLB_txt(txt)
    self.attrLB:dftext(txt):dfshow()
end
